// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   Main.java
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>
package essaw4aadtextracter;

// <editor-fold defaultstate="collapsed" desc="imports">
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import uoc.libwave.WaveFormatPCMShort;
import uoc.libwavelets.Daubechies.Daubechies;
// </editor-fold>

/**
 * example017.py
 *
 * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * @version 2.0
 * @since 2013-08-16
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2) {
            System.err.println("Must supply filenames:");
            System.err.println("java -jar PCM-S16-LE-ESSAW4AADT-extracter.jar"
                    + " input.wav output.wav");
            System.exit(1);
        }

        String strSrcFilePath = args[0];
        String strDstFilePath = args[1];

        try {
            // Load the wave file
            WaveFormatPCMShort waveData = new WaveFormatPCMShort(strSrcFilePath);

            // Variables initalization
            Daubechies db10 = new Daubechies(10);
            Daubechies auxdb10 = new Daubechies(10);
            Watermarking watermarking = new Watermarking("Hello World!");
            System.out.println(watermarking.toString());
            double storeFilteredSample = 0;
            double skipFilteredSample;
            double watermark;
            double rHPF, rLPF;
            boolean state = true;
            short wChannel = (short) 1;
            short sample;
            int w = 0;
            int num_samples = waveData.getNumSamples();
            short sync = (short) 0xFFFF;
            int size = 0;

            // compress data
            for (int i = 0; i < num_samples; i++) {
                sample = waveData.getSample(wChannel, i);
                if (state == true) {
                    // DWT: LPFd
                    storeFilteredSample = db10.dLPF.filterSample(sample);
                    watermark = db10.dHPF.filterSample(sample);
                    // IDWT: LPF + Watermarking: Sync + Data
                    rLPF = db10.rLPF.filterSample((short) storeFilteredSample);
                    waveData.setSample(wChannel, i, (short) rLPF);

                    state = false;
                } else {
                    // DWT: Skip
                    skipFilteredSample = db10.dLPF.filterSample(sample);
                    watermark = db10.dHPF.filterSample(sample);
                    // IDWT: (upsample) LPF + Watermarking: Sync + Data
                    rLPF = db10.rLPF.filterSample((short) 0);
                    waveData.setSample(wChannel, i, (short) rLPF);

                    state = true;
                }
                System.out.print((short) watermark + ",");
            }

            // test 2
            System.out.println("");
            state = true;
            w = 0;
            for (int i = 0; i < num_samples; i++) {
                if (state == true) {
                    short aux = (short) watermarking.getWatermark(w);
                    rHPF = auxdb10.rHPF.filterSample(aux);
                    if (w >= watermarking.size() - 1) {
                        w = 0;
                    } else {
                        w += 1;
                    }
                    state = false;
                } else {
                    rHPF = auxdb10.rHPF.filterSample((short) 0);
                    state = true;
                }
                System.out.print((short) auxdb10.dHPF.filterSample((short) rHPF) + ",");

            }


            // save the wave file
            waveData.saveToFile(strDstFilePath);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
